package com.acce.gxsc.page.supervision.makeupCar;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.acce.gxsc.R;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.model.netBean.InGoodsBean;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.adapterlv.CommonAdapter;
import com.acce.sdk.widget.adapterlv.ViewHolder;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */

public class SupervisionMakeupCarActivity extends BaseMvpActivity<SupervisionMakeupCarView, SupervisionMakeupCarPresenter> implements SupervisionMakeupCarView {

    ArrayList<String> mDiliveryTypeData;
    ArrayList<String> mGoodsTypeData;

    @Bind(R.id.accebar)
    AcceBar  mAccebar;
    @Bind(R.id.tv_time)
    TextView mTvTime;
    @Bind(R.id.tv_sure)
    TextView mTvSure;
    @Bind(R.id.spn_dilivery_type)
    Spinner  mSpnDiliveryType;
    @Bind(R.id.et_plate_num_g)
    EditText mEtPlateNumG;
    @Bind(R.id.et_plate_num_x)
    EditText mEtPlateNumX;
    @Bind(R.id.et_contain_no)
    EditText mEtContainNo;
    @Bind(R.id.et_hang_num)
    EditText mEtHangNum;
    @Bind(R.id.et_contact_name)
    EditText mEtContactName;
    @Bind(R.id.et_under_company)
    EditText mEtUnderCompany;
    @Bind(R.id.spn_goods_type)
    Spinner  mSpnGoodsType;
    @Bind(R.id.et_goods_info)
    EditText mEtGoodsInfo;
    @Bind(R.id.cb_fee2_id)
    CheckBox mCbFee2Id;
    @Bind(R.id.cb_fee3_id)
    CheckBox mCbFee3Id;
    @Bind(R.id.et_supervise_remarks)
    EditText mEtSuperviseRemarks;
    @Bind(R.id.tv_create_date)
    TextView mTvCreateDate;
    @Bind(R.id.tv_create_time)
    TextView mTvCreateTime;

    @Override
    protected SupervisionMakeupCarPresenter createPresenter() {
        return new SupervisionMakeupCarPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.supervision_makeup_car_activity);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        mDiliveryTypeData = new ArrayList<>();
        mDiliveryTypeData.add("请选择");
        mDiliveryTypeData.add("整柜-私人柜");
        mDiliveryTypeData.add("整柜-月结公司");
        mDiliveryTypeData.add("整柜-国冷、通泰、卓志");
        mDiliveryTypeData.add("散货-国冷、通泰、卓志");
        mDiliveryTypeData.add("散货-空运货");
        mDiliveryTypeData.add("散货-精品货");
        mDiliveryTypeData.add("整/散-国产货");
        mDiliveryTypeData.add("登记-拼货车辆");
        mDiliveryTypeData.add("登记-场地费");
        mDiliveryTypeData.add("登记-机动位");
        mDiliveryTypeData.add("登记-采购车辆");
        mDiliveryTypeData.add("错单修改");
        mGoodsTypeData = new ArrayList<>();
        mGoodsTypeData.add("请选择");
        mGoodsTypeData.add("西货普货");
        mGoodsTypeData.add("西货特货");
        mGoodsTypeData.add("东南亚普货");
        mGoodsTypeData.add("东南亚特货");
        mGoodsTypeData.add("国产精品普货");
        mGoodsTypeData.add("国产精品特货");
        mGoodsTypeData.add("进口礼品装");
        mGoodsTypeData.add("国产礼品装");
        mGoodsTypeData.add("鑫荣懋过车");
        mSpnDiliveryType.setAdapter(new CommonAdapter<String>(this, R.layout.input_spinner_item_1, mDiliveryTypeData) {
            @Override
            protected void convert(ViewHolder viewHolder, String s, int i) {
                viewHolder.setText(R.id.tv_content, s);
            }
        });
        mSpnGoodsType.setAdapter(new CommonAdapter<String>(this, R.layout.input_spinner_item_1, mGoodsTypeData) {
            @Override
            protected void convert(ViewHolder viewHolder, String s, int i) {
                viewHolder.setText(R.id.tv_content, s);
            }
        });
        mTvTime.setText(DateUtils.getTime("MM-dd HH:mm"));
    }

    public String getDiliveryType() {
        int position = mSpnDiliveryType.getSelectedItemPosition();
        if (position == 0) {
            return "";
        } else {
            return mDiliveryTypeData.get(position);
        }
    }

    public String getGoodsType() {
        int position = mSpnGoodsType.getSelectedItemPosition();
        if (position == 0) {
            return "";
        } else {
            return mGoodsTypeData.get(position);
        }
    }

    public void doSure() {
        final String diliveryType = getDiliveryType();
        final String plateNumG = mEtPlateNumG.getText().toString();
        final String plateNumX = mEtPlateNumX.getText().toString();
        final String containNo = mEtContainNo.getText().toString();
        final String hangNum = mEtHangNum.getText().toString();
        final String contactName = mEtContactName.getText().toString();
        final String underCompany = mEtUnderCompany.getText().toString();
        final String fee2Id = mCbFee2Id.isChecked() ? "1" : "0";
        final String fee3Id = mCbFee3Id.isChecked() ? "1" : "0";
        final String superviseRemarks = mEtSuperviseRemarks.getText().toString();
        final String createDate = mTvCreateDate.getText().toString();
        final String createTime = mTvCreateTime.getText().toString();
        final ArrayList<InGoodsBean> pregistergoods = new ArrayList<>();
        InGoodsBean goodsBean = new InGoodsBean();
        goodsBean.goodsType = getGoodsType();
        goodsBean.goodsName = getGoodsType();
        pregistergoods.add(goodsBean);
        if (StringUtils.isEmpty(getDiliveryType())) {
            ToastUtils.show("请选择登记类型！");
            return;
        }
        if (StringUtils.isEmpty(createDate) || StringUtils.isEmpty(createTime)) {
            ToastUtils.show("请选择时间！");
            return;
        }
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定补录？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.makeupCar(diliveryType, plateNumG, plateNumX, containNo, hangNum, contactName, underCompany,
                                pregistergoods, fee2Id, fee3Id, superviseRemarks, createDate + " " + createTime + ".000");
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    @Override
    public void makeupCarSuccess() {
        finish();
    }

    @OnClick({R.id.tv_create_date, R.id.tv_create_time, R.id.tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_create_date:
                selectCreateDate();
                break;
            case R.id.tv_create_time:
                selectCreateTime();
                break;
            case R.id.tv_sure:
                doSure();
                break;
        }
    }

    private int[] mCurrentDate;

    private void selectCreateTime() {
        int[] times = DateUtils.getCurrentTimes();
        TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                mTvCreateTime.setText(String.format("%02d:%02d:00", i, i1));
            }
        }, times[0], times[1], true);
        dialog.setTitle("请选择时间");
        dialog.show();
    }

    private void selectCreateDate() {
        mCurrentDate = DateUtils.getCurrentDates();
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                mTvCreateDate.setText(String.format("%02d-%02d-%02d", year, monthOfYear + 1, dayOfMonth)/*MessageFormat.format("{0} - {1} - {2}", year, monthOfYear + 1, dayOfMonth)*/);
            }
        }, mCurrentDate[0], mCurrentDate[1], mCurrentDate[2]);
        dialog.getDatePicker().setMinDate(new GregorianCalendar(2015, 11, 1).getTimeInMillis());
        dialog.getDatePicker().setMaxDate(new GregorianCalendar(mCurrentDate[0], mCurrentDate[1] - 1, mCurrentDate[2]).getTimeInMillis());
        dialog.setTitle("请选择日期");
        dialog.show();
    }


}
